﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using WXDba.Entity;
using WXDba.Abstract;
using System.Data.Entity;

namespace WXDba.Factory
{
    internal class UserGroupOperate : BaseOperate, IUserGroup
    {
        #region 基础访问  
        /// <summary>  
        /// 获取表达式树  
        /// </summary>  
        /// <returns></returns>  
        public DbSet<UserGroup> GetDbSet()
        {
            return Context.UserGroups;
        }
        /// <summary>  
        /// 获取对象  
        /// </summary>  
        /// <param name="ID">主键ID</param>  
        /// <returns></returns>  
        public UserGroup GetModel(int ID)
        {
            return GetDbSet().Find(ID);
        }
        /// <summary>  
        /// 添加对象  
        /// </summary>  
        /// <param name="model">对象</param>  
        /// <returns></returns>  
        public bool Add(UserGroup model)
        {
            if (IsValided(model) == false)
                return false;
            try
            {
                GetDbSet().Add(model);
                return Save() > 0;
            }
            catch (Exception ex)
            {
                return Error_Add(ex);
            }
        }
        /// <summary>  
        /// 修改对象  
        /// </summary>  
        /// <param name="model">对象</param>  
        /// <returns></returns>  
        public bool Modify(UserGroup model)
        {
            if (IsValided(model) == false)
                return false;
            try
            {
                return SetModify(model);
            }
            catch (Exception ex)
            {
                return Error_Modify(ex);
            }
        }
        /// <summary>  
        /// 删除对象  
        /// </summary>  
        /// <param name="model">对象</param>  
        /// <returns></returns>  
        public bool Delete(UserGroup model)
        {
            try
            {
                GetDbSet().Remove(model);
                return Save() > 0;
            }
            catch (Exception ex)
            {
                return Error_Delete(ex);
            }
        }
        /// <summary>  
        /// 删除对象  
        /// </summary>  
        /// <param name="ID">主键ID</param>  
        /// <returns></returns>  
        public bool Delete(int ID)
        {
            UserGroup model = GetModel(ID);
            if (model == null)
                throw new Exception(string.Format("删除对象时，获取ID={0}的对象失败", ID));
            return Delete(model);
        }
        #endregion

        #region 扩展接口
        /// <summary>
        /// 获取指定分组的用户数量
        /// </summary>
        /// <param name="ID">主键ID</param>
        /// <returns></returns>
        public int GetUserCount(int ID)
        {
            UserGroup model = GetModel(ID);
            if (model == null)
                throw new Exception(string.Format("获取分组用户数量时，获取ID={0}的对象失败", ID));
            return model.Count;
        }
        /// <summary>
        /// 清空本地分组
        /// </summary>
        /// <returns></returns>
        public bool Clear()
        {
            GetDbSet().RemoveRange(GetDbSet());
            return Save() > 0;
        }
        /// <summary>
        /// 重新替换分组
        /// </summary>
        /// <param name="groups">分组列表</param>
        /// <returns></returns>
        public bool Replace(List<UserGroup> groups)
        {
            //1.清空
            GetDbSet().RemoveRange(GetDbSet());
            //2.重新添加
            foreach (var item in groups)
            {
                GetDbSet().Add(new UserGroup()
                {
                    ID = item.ID,
                    Name = item.Name,
                    Count = item.Count,
                    AddTime = DateTime.Now
                });
            }
            return Save() > 0;
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public List<UserGroup> GetList()
        {
            return GetDbSet().ToList();
        }
        /// <summary>
        /// 修改本地分组名称
        /// </summary>
        /// <param name="ID">分组ID</param>
        /// <param name="Name">分组名称</param>
        /// <returns></returns>
        public bool UpdateName(int ID, string Name)
        {
            UserGroup model = GetModel(ID);
            if (model == null)
                return false;
            model.Name = Name;
            return Modify(model);
        }
        #endregion

        #region 获取数量
        /// <summary>
        /// 获取分组总数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return GetDbSet().Count();
        }
        #endregion
    }
}
